/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_te_packet_unfragment.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2017/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include <tongeasy/config.h>
#include <tongeasy/te_core.h>
#include <tongeasy/te_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include "../test_tongeasy_comm.h"

extern void __te_destroy_packet(L7_te_packet **pp_st_te_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    p_st_te_packet->fragment_state = APM_TE_FRAGMENT_NEED_NEXT_REQ_PACKET;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_te_packet->st_timestamp_last.tv_sec, 1454549354L);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
}

static void __case2(void)
{
    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    p_st_te_packet->fragment_state = APM_TE_FRAGMENT_NEED_NEXT_RESP_PACKET;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_te_packet->st_timestamp_last.tv_sec, 1454549354L);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
}

static void __case3(void)
{
    char *p_bin = (char *) "cf0700000a6fd2001421000064656d6f636c6900000000000064656d6f737672000000000000000033080000355a303231307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e312020202020202030303235202030202020302020203130202033302020746869732073796e7376722064617461207265715f66756e31";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    p_st_te_packet->fragment_state = APM_TE_FRAGMENT_NEED_NEXT_RESP_PACKET;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_te_packet->st_timestamp_last.tv_sec, 1454549354L);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
}

static void __case4(void)
{
    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e632064617461";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    p_st_te_packet->fragment_state = APM_TE_FRAGMENT_NEED_NEXT_REQ_PACKET;
    p_st_te_packet->ui_len_to_read = 1500;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_NEED_NEXT_PACKET);
    CU_ASSERT_EQUAL(p_st_te_packet->st_timestamp_last.tv_sec, 1454549354L);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
}

static void __case5(void)
{
    char *p_bin = (char *) "cf0700000a6fce009325000064656d6f73767200000000000064656d6f636c690000000000000000b2080000355a303230307c30303037303030307c30303434303030307c64656d6f636c69202020202064656d6f636c69202020202064656d6f73767220202020203733202020203020202064656d6f636c6920202020203020202000000000000000000000000063616c6c5f7265715f66756e3120202020202020303032312020302020203020202031302020333020207468697320636c69656e742073796e6320646174";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_TE_SRC_IP, TEST_TE_DST_IP, TEST_TE_SRC_PORT, TEST_TE_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_te_packet *p_st_te_packet = NULL;
    p_st_te_packet = (L7_te_packet *) frmalloc(sizeof(L7_te_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_te_packet);
    memset(p_st_te_packet, 0x00, sizeof(L7_te_packet));

    CU_ASSERT_EQUAL(te_init_packet(p_st_te_packet), FR_SUCC);

    p_st_te_packet->fragment_state = APM_TE_FRAGMENT_NEED_NEXT_RESP_PACKET;
    int i_payload_offset = 0;

    CU_ASSERT_EQUAL(te_packet_unfragment(p_st_packet, p_st_te_packet, i_payload_offset), APM_TE_CHECK_FRAGMENT_NEED_NEXT_PACKET);
    CU_ASSERT_EQUAL(p_st_te_packet->st_timestamp_last.tv_sec, 0L);

    __te_destroy_packet(&p_st_te_packet);
    CU_ASSERT_PTR_NULL(p_st_te_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
}

int add_suite_te_packet_unfragment(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" te_packet_unfragment()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
